<?php
	
	require_once('phing/Task.php');
	
	class NextPointVersionTask extends Task {
		
	     
	    private $returnProperty;		
		private $repo_url;
		
		public function main(){			
			$retval = $this->getNextPointVersion($this->project->getProperty('stable_version'),
				$this->project->getProperty('repo_url'),
				$this->project->getProperty('repo_secure_url')
				);			
			$this->project->setProperty($this->returnProperty, $retval);
		}
		
		private function getNextPointVersion($stable_version,$repo_url,$repo_secure_url){
			
			$tagged_versions_url = $repo_url . "/tags/" . $stable_version; 
					
			$raw_point_versions = svn_ls($tagged_versions_url); 
			$point_versions = array();
			foreach($raw_point_versions as $tmp_point_version){
					$tmp_name = $tmp_point_version['name'];
					//$this->log($tmp_name);
					$tmp_part = substr($tmp_name,strrpos($tmp_name,".") + 1);
					//$this->log($tmp_part);
					$point_versions[] = $tmp_part;	
				
			}
			
			asort($point_versions);
			
			$next_point_version	= (int)$point_versions[count($point_versions) - 1] + 1;
			
			$next_version = $stable_version . "." .$next_point_version;
			$this->log("Next version is ".$next_version);
			
			$src_url = $repo_secure_url . "/branches/releases/".$stable_version;
			$dest_url = $repo_secure_url . "/tags/".$stable_version."/" . $next_version;
			
			$command = "svn copy ".$src_url." ".$dest_url. " -m \"Creating point version ".$next_version."\"";
			
			$code = null;
			$output = null;
			$this->log($command);
			
			$out = exec($command,$output,$code);
			
			if($code != 0){
				throw new Exception("Could not perform svn copy");
			} else {			
				return $next_version;
			}
		}
		
		
		public function setReturnProperty($r) {
       		$this->returnProperty = $r;
    	}
    	    	
    	public function getReturnProperty(){
    		return $this->returnProperty;
    	}
    
	} 


?>
